<template>
	<view>
		
		<!-- <camera device-position="back" mode="scanCode" bindscancode="scancode" flash="off" binderror="error" style="width: 100%; height: 300px;"></camera>
		<button type="primary" @click="takePhoto">拍照</button>
		<view>预览</view>
		<image mode="widthFix" :src="src"></image> -->
		
		<image class="logo"  @click="nfc" src="/static/nfc%20(1).png"></image>
		<view class="text-area">
			<text class="title" @click="nfc">{{title}}</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				src:null
			}
		},
		methods: {
			takePhoto:function() {
			    wx.scanCode({
			      success (res) {
			        console.log(res)
			      }
			    })
			  },
			error:function(e) {
			    console.log(e.detail)
			  },
			scancode:function(e){
				  console.log(e.detail)
			  },
			  
			  nfc:function(){
			          
			        /**
			       * 字节对象转字符串
			       * @param {Object} arr
			       */
			      const byteToString = function(arr) {
			          if (typeof arr === 'string') {
			              return arr;
			          }
			  
			          var str = '',
			              _arr = arr;
			  
			          for (var i = 0; i < _arr.length; i++) {
			              var one = _arr[i].toString(2),
			                  v = one.match(/^1+?(?=0)/);
			  
			              if (v && one.length == 8) {
			                  var bytesLength = v[0].length;
			  
			                  var store = _arr[i].toString(2).slice(7 - bytesLength);
			  
			                  for (var st = 1; st < bytesLength; st++) {
			                      store += _arr[st + i].toString(2).slice(2);
			                  }
			  
			                  str += String.fromCharCode(parseInt(store, 2));
			  
			                  i += bytesLength - 1;
			              } else {
			                  str += String.fromCharCode(_arr[i]);
			              }
			          }
			          return str;
			      };
			  
			      /**
			       * 字符串转字节
			       * @param {Object} str
			       */
			      const string2ArrayBuffer = function(str) {
			          // 首先将字符串转为16进制
			          let val = '';
			          for (let i = 0; i < str.length; i++) {
			              if (val === '') {
			                  val = str.charCodeAt(i).toString(16);
			              } else {
			                  val += ',' + str.charCodeAt(i).toString(16);
			              }
			          }
			          // 将16进制转化为ArrayBuffer
			          return new Uint8Array(
			              val.match(/[\da-f]{2}/gi).map(function(h) {
			                  return parseInt(h, 16);
			              })
			          ).buffer;
			      };
			      /**
			       * 格式化得到aid值
			       * @param {Object} buffer
			       */
			      const ab2hex = function(buffer) {
			          var hexArr = Array.prototype.map.call(
			              new Uint8Array(buffer),
			  
			              function(bit) {
			                  return ('00' + bit.toString(16)).slice(-2);
			              }
			          );
			          return hexArr.join('');
			      };
			      
			      //获取NFCAAdapter实例
			      const NFCAdapter = wx.getNFCAdapter();
			      //     通过NFCAdapter.getNfcA()获取NfcA实例
			      const Ndef = NFCAdapter.getNdef()
			  //     调用NFCAdapter.onDiscovered(function callback)注册贴卡监听回调
			      // NFCAdapter.onDiscovered(res=>{
			      //     console.log("注册监听回调")
			      //     console.log(res);
			      // })
			  //     调用NFCAdapter.startDiscovery(Object object)开始监听贴卡
			      NFCAdapter.startDiscovery({
			          success:res=>{
			              console.log("监听贴卡成功")
			              console.log(res)
			  			uni.showLoading({
			  			    title: '请贴近nfc卡片'
			  			});
			          },
			          fail:err=>{
			              console.log("监听贴卡失败")
			              console.log(err)
			  			uni.showToast({
			  				icon:"error",
			  			    title: '本机不支持nfc签到',
			  			    duration: 2000
			  			});
			          }
			      })
			  //     贴卡，onDiscovered回调
			      NFCAdapter.onDiscovered(res=>{
			          console.log("贴卡监听回调")
			          console.log(res)
			          //     使用NfcA实例进行读写
			          //     调用NfcA.connect()和NFC卡片建立连接
			              Ndef.connect({
			                  success:res=>{
			                      console.log("连接nfc卡片成功");
			                      console.log(res)
			                      const records = [
			                          {
			                              id: string2ArrayBuffer("1001"),
			                              payload: string2ArrayBuffer("M304"),
			                              type: string2ArrayBuffer("U"),
			                              tnf: 1
			                          }
			                      ];
			                      //调用NfcA.transceive(Object object)往NFC卡片写入apdu指令并接收卡片返回数据
			                      Ndef.writeNdefMessage({
			                          // uris:string2ArrayBuffer("my.531party.cn"),
			                          records:records,
			                          success:res=>{
			                              console.log("写入成功")
			                              console.log(res)
			                          },
			                          fail:err=>{
			                              
			                              console.log("写入失败")
			                              console.log(string2ArrayBuffer("1001"))
			                              console.log(err)
			                          }
			                      })
			                  },
			                  fail:err=>{
			                      console.log("连接nfc卡片失败")
			                      console.log(err)
			                  }
			              })
			      })
			  //     根据onDiscovered回调res对象的techs字段匹配到卡片支持NFC-A标准
			  
			  
			  //     调用NfcA.transceive(Object object)往NFC卡片写入apdu指令并接收卡片返回数据
			  //     读写完毕，调用NfcA.close()断开连接
			      // NFCA.close({
			      //     success:res=>{
			      //         console.log("断开nfc卡片成功");
			      //         console.log(res)
			      //     },
			      //     fail:err=>{
			      //         console.log("断开nfc卡片失败")
			      //         console.log(err)
			      //     }
			      // })
			  //     调用NFCAdapter.stopDiscovery(Object object)结束监听贴卡
			      // NFCAdapter.startDiscovery({
			      //     success:res=>{
			      //         console.log("结束监听成功");
			      //         console.log(res)
			      //     },
			      //     fail:err=>{
			      //         console.log("结束监听失败");
			      //         console.log(err)
			      //     }
			      // })
			      
			          
			          
			      },
		},
		onLoad() {
			//查询登陆状态
			this.checkLogin();
		}
	}
</script>

<style>

</style>
